<?php
/**
  +------------------------------------------------------------------------------
 * 缓存管理
  +------------------------------------------------------------------------------
 */
class CacheAction extends CommonAction {
    /**
     *系统缓存清理界面 
     */
    public function system() {
        $this->display();
    }
    
    public function clear() {
        $type = trim($_REQUEST['type']);
        switch ($type) {
            case 'system':
                $is_data = isset($_REQUEST['is_data']) ? intval($_REQUEST['is_data']) : 0;
                $is_tpl = isset($_REQUEST['is_tpl']) ? intval($_REQUEST['is_tpl']) : 0;
                $is_s = isset($_REQUEST['is_s']) ? intval($_REQUEST['is_s']) : 0;
                $is_html = isset($_REQUEST['is_html']) ? intval($_REQUEST['is_html']) : 0;
                $bln = false;
                unset($_SESSION['cache_system_clear']);
                if ($is_data == 1) {
                    $bln = true;
                    $_SESSION['cache_system_clear']['is_data'] = 1;
                }

                if ($is_tpl == 1) {
                    $bln = true;
                    $_SESSION['cache_system_clear']['is_tpl'] = 1;
                }

                if ($is_s == 1) {
                    $bln = true;
                    $_SESSION['cache_system_clear']['is_s'] = 1;
                }
                
                if ($is_html == 1) {
                    $bln = true;
                    $_SESSION['cache_system_clear']['is_html'] = 1;
                }

                if (!$bln)
                    $this->redirect('Cache/system');
                else
                    $this->redirect('Cache/systemClear');
                break;
            case 'xiangmu':
                $is_admin = isset($_REQUEST['is_admin']) ? intval($_REQUEST['is_admin']) : 0;
                $bln = false;
                
                
                
                if ($is_admin == 1) {
                    $bln = true;
                    $_SESSION['cache_xiangmu_clear']['is_admin'] = 1;
                }
                
                if (!$bln)
                    $this->redirect('Cache/cleardata');
                else
                    $this->redirect('Cache/cleardata_x');
                break;

            case 'custom':
                $file = isset($_REQUEST['file']) ? trim($_REQUEST['file']) : '';
                unset($_SESSION['cache_custom_clear']);
                if (empty($file) || !file_exists($file))
                    $this->redirect('Cache/custom');
                else {
                    if (!is_dir($file)) {
                        @unlink($file);
                        $this->redirect('Cache/custom');
                    } else {
                        $_SESSION['cache_custom_clear'] = array('root' => $file, 'current' => $file);
                        $this->redirect('Cache/customClear');
                    }
                }
                break;

            default:
                $this->redirect('Cache/system');
                break;
        }
    }

    public function systemClear() {
        $system_clear = $_SESSION['cache_system_clear'];
        if (empty($system_clear))
            $this->redirect('Cache/system');
        else
            $_SESSION['cache_system_clear'] = $system_clear;

        @set_time_limit(3600);
        if (function_exists('ini_set')) {
            ini_set('max_execution_time', 3600);
            ini_set("memory_limit", "256M");
        }

        $this->display();

        ob_start();
        ob_end_flush();
        ob_implicit_flush(1);
        
        import('ORG.Io.Dir');
        $Dir=new Dir();
        if (isset($system_clear['is_data'])) {
            if(is_dir(DATA_PATH)) $Dir->delDir(DATA_PATH);
            echoFlush('<script type="text/javascript">showmessage(\'' . L('SYSTEM_TIPS9') . '\',1);</script>');
            usleep(1500000);
        }
        
        
        if (isset($system_clear['is_tpl'])) {
            
            if(is_dir(CACHE_PATH)) $Dir->delDir(CACHE_PATH);
            echoFlush('<script type="text/javascript">showmessage(\'' . L('SYSTEM_TIPS8') . '\',1);</script>');
            usleep(1500000);
        }
        
        
        if (isset($system_clear['is_s'])) {
            if(is_dir(TEMP_PATH)) $Dir->delDir(TEMP_PATH);
            echoFlush('<script type="text/javascript">showmessage(\'' . L('SYSTEM_TIPS7') . '\',1);</script>');
            usleep(1500000);
        }
        
        if (isset($system_clear['is_html'])) {
            if(is_dir(HTML_PATH)) $Dir->delDir(HTML_PATH);
            echoFlush('<script type="text/javascript">showmessage(\'' . L('SYSTEM_TIPS6') . '\',1);</script>');
            usleep(1500000);
        }

        
        echoFlush('<script type="text/javascript">showmessage(\'' . L('SYSTEM_TIPS9') . '\',3);</script>');
        exit;
    }
    
    
    
    public function cleardata_x() {
        $system_clear = $_SESSION['cache_xiangmu_clear'];
        
        if (empty($system_clear))
            $this->redirect('Cache/cleardata');
        else
            $_SESSION['cache_xiangmu_clear'] = $system_clear;

        @set_time_limit(3600);
        if (function_exists('ini_set')) {
            ini_set('max_execution_time', 3600);
            ini_set("memory_limit", "256M");
        }

        $this->display();

        ob_start();
        ob_end_flush();
        ob_implicit_flush(1);

        if (isset($system_clear['is_admin'])) {
            Vendor('common');
            usleep(50);
            Dir::delDir(FANWE_ROOT . 'public/data/admin/runtime');
            @mkdir(FANWE_ROOT . './public/data/admin/runtime', 0777);
            @chmod(FANWE_ROOT . './public/data/admin/runtime', 0777);
            usleep(50);
            echoFlush('<script type="text/javascript">showmessage(\'' . L('项目编译缓存清理完成！') . '\',1);</script>');
        }
        usleep(100);

        echoFlush('<script type="text/javascript">showmessage(\'' . L('清理项目编译缓存成功！') . '\',3);</script>');
        exit;
    }
    



}

?>